﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Data;
using System.Collections;
using System.Windows.Controls.Primitives;
using MyPublisher.Windows.Controls.Primitives;

namespace MyPublisher.Windows.Controls
{
    public class AutoOverflowToolBar : ToolBar
    {
        private ToggleButton _overFlowToggleButton;

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            ToggleButton toggleButton = GetTemplateChild("OverflowButton") as ToggleButton;
            if (toggleButton != null)
            {
                _overFlowToggleButton = toggleButton;
                //Auto hide overflow button, if there is no overflow items
                Binding overFlowVisibilityBinding = new Binding("HasOverflowItems");
                overFlowVisibilityBinding.Source = this;
                overFlowVisibilityBinding.Mode = BindingMode.OneWay;
                overFlowVisibilityBinding.Converter = DataConverterBool2Visibility.Instance;
                _overFlowToggleButton.SetBinding(UIElement.VisibilityProperty, overFlowVisibilityBinding);
            }
            //Auto hide toolbar, if there is no items in toolbar
            Binding visibilityBinding = new Binding("HasItems");
            visibilityBinding.Source = this;
            visibilityBinding.Mode = BindingMode.OneWay;
            visibilityBinding.Converter = DataConverterBool2Visibility.Instance;
            this.SetBinding(UIElement.VisibilityProperty, visibilityBinding);
        }
    }
}
